/*
 * Javasript based on Functions(JBF)
 * 
 * Copyright(c) 2010, Fooxee (JA86.CN)
 *
 * JBF is licensed under the terms of the Open Source GPL 3.0(http://www.gnu.org/licenses/gpl.html) license. 
 * 
 * JBF's blog: http://www.cnblogs.com/JBF/
 * 
 * JBF's project: http://jbfproject.googlecode.com/
 *
 * Created by Fooxee. at 2010.03.30  mailTo:hjccjun@163.com
 */﻿
 
 
/*

#summary JBF.JSON 主文档.

= JBF.JSON =

对象:JBF.JSON

文件:base/JSON.js

描述:Json静态类.

|| [#Public_Config 配置项] || [#Public_Properties 属性] || [#Public_Methods 方法] || [#Public_Events 事件] ||

=== Public Config ===
公开配置


=== Public Properties ===
公开属性



=== Public Methods ===
公开方法


*/
JBF.JSON = {
	
	/*
	*jsonDecode*(String s):Object
	JSON解码.<br>
	参数
		|| *参数名* || *值类型* || *描述* ||
		|| s	|| String 	|| JSON字符串 ||
	
	*返回*:{Object} JSON <br>
	<hr>
	*/
	decode : function(s)
	{
		return eval("(" + s + ')');
	},
	
	/*
	*jsonEncode*(Object o):String
	JSON编码.<br>
	参数
		|| *参数名* || *值类型* || *描述* ||
		|| o	|| Object 	|| JSON对象 ||
	
	*返回*:{String} JSON字符串 <br>
	<hr>
	*/
	encode : function(o)
	{
		if(!JBF.isDefined(o) || o === null)
			return "null";
		else if(JBF.isString(o))
			return '"'+o+'"';
		else if(JBF.isNumber(o))
			return o;
		else if(JBF.isBoolean(o))
			return o;
		else if(JBF.isDate(o))
			return this.ecDate(o);
		else if(JBF.isArray(o))
			return this.ecArray(o);
		else{
		var a = ["{"], b, i, v;
		for (i in o)
		{
			v = o[i];
			switch (typeof v)
			{
				case "undefined":
				case "function":
				case "unknown":
					break;
				default:
					if(b)
						a.push(',');
					a.push(this.encode(i), ":", v === null ? "null" : this.encode(v));
					b = true;
			}
		}
		a.push("}");
		return a.join("");
		}
		
			
	},
	//private
	ecDate : function(o)
	{
		var pad = function(n)
		{
            return n < 10 ? "0" + n : n;
        }
		return '"' + o.getFullYear() + "-" +
                pad(o.getMonth() + 1) + "-" +
                pad(o.getDate()) + " " +
                pad(o.getHours()) + ":" +
                pad(o.getMinutes()) + ":" +
                pad(o.getSeconds()) + '"';
	},
	//private
	ecArray : function(o)
	{
		var a = ["["], b, i, l = o.length, v;
		for (i = 0; i < l; i += 1)
		{
			v = o[i];
			switch (typeof v)
			{
				case "undefined":
				case "function":
				case "unknown":
					break;
				default:
					if(b)
						a.push(',');
					a.push(v === null ? "null" : this.encode(v));
					b = true;
			}
		}
		a.push("]");
		return a.join("");
	}
};












